package com.mcp1993.retorfit2demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {


    private Button button;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        textView = (TextView) findViewById(R.id.text);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("https://api.thinkpage.cn")
                        .addConverterFactory(GsonConverterFactory.create())
                        .client(new OkHttpClient())
                        .build();
                WeatherService weatherService = retrofit.create(WeatherService.class);
                //@Query 动态设置请求参数
                Call<WeatherBean> call = weatherService.weather("rot2enzrehaztkdk","beijing");

              /*  //使用@QueryMap 动态设置请求参数
                Map<String,String> key = new HashMap<>();
                key.put("key","rot2enzrehaztkdk");
                Map<String,String> location = new HashMap<>();
                location.put("location","beijing");
                Call<WeatherBean> call2 = weatherService.weather(key,location);
            */

               /* //修改interface中的路径@path
                Call<WeatherBean> call3 = weatherService.weather("weather","rot2enzrehaztkdk","beijing");
                */
                call.enqueue(new Callback<WeatherBean>() {
                    @Override
                    public void onResponse(Call<WeatherBean> call, Response<WeatherBean> response) {
                        WeatherBean weatherBean = response.body();
                        Log.e("mcp",weatherBean.getResults().get(0).getNow().getText());
                        textView.setText(weatherBean.getResults().get(0).getNow().getText());
                    }

                    @Override
                    public void onFailure(Call<WeatherBean> call, Throwable t) {
                        Log.e("mcp", "Error" + t.toString());
                    }
                });
            }
        });
    }
}
